package com.huqing.icu.protocol;

import com.huqing.icu.exceptions.BusinessException;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
public class BaseResponse<T> {

    @Schema(name = "服务器时间", example = "1542183561830")
    private long serverTime;

    @Schema(name = "调用成功的时返回的数据")
    private T data;

    private BaseStateMap state;

    public static <T> BaseResponse<T> valueOfSuccess(T data) {
        BaseResponse<T> baseResponse = new BaseResponse<>();
        baseResponse.setData(data);
        baseResponse.setState(new BaseStateMap());
        baseResponse.setServerTime(System.currentTimeMillis());
        return baseResponse;
    }

    public static <T> BaseResponse<T> valueOfSuccess() {
        BaseResponse<T> baseResponse = new BaseResponse<>();
        baseResponse.setState(new BaseStateMap());
        baseResponse.setServerTime(System.currentTimeMillis());
        return baseResponse;
    }

    public static <T> BaseResponse<T> valueOfError(BusinessException e) {
        return valueOfError(e.getCode(), e.getMessage());
    }

    public static <T> BaseResponse<T> valueOfSysError() {
        return valueOfError(SystemResponseCode.INTERNAL_ERROR.getCode(), SystemResponseCode.INTERNAL_ERROR.getMsg());
    }

    public static <T> BaseResponse<T> valueOfError(int code, String msg) {
        BaseResponse<T> baseResponse = new BaseResponse<>();
        baseResponse.setServerTime(System.currentTimeMillis());
        BaseStateMap stateMap = new BaseStateMap(code, msg);
        baseResponse.setState(stateMap);
        return baseResponse;
    }

    public static <T> BaseResponse<T> valueOfError(int code, String msg, T data) {
        BaseResponse<T> baseResponse = new BaseResponse<>();
        baseResponse.setServerTime(System.currentTimeMillis());
        BaseStateMap stateMap = new BaseStateMap(code, msg);
        baseResponse.setState(stateMap);
        return baseResponse;
    }
}
